var InputEntity = (function() {
	var InputEntity = function() {};

	// 文本输入框
	InputEntity.ExTextArea = (function() {
		var ExTextArea = function(elem, placeHolder, showId, maxLen) {
			this.elem = elem;					// 设置对象
			this.placeHolder = placeHolder;		// 设置placeholder
			this.showId = showId;				// 显示字数提示的DOM对象id
			this.maxLen = maxLen || 70;				

			var _this = this;
			elem.onfocus = function() { _this.onFocus(); }
			elem.onblur = function() { _this.onBlur(); }
			elem.onkeyup = function() { _this.check(); }
			elem.onmousedown = function() { _this.check(); }
		};
	
		ExTextArea.prototype = {
			// 输入框得到焦点时
			onFocus: function() {
				if (this.elem.value == this.placeHolder) {
					this.elem.value = "";
				}
			},			
			// 输入框失去焦点时
			onBlur: function() {
				if (this.elem.value == "" || this.elem.value.replace(/\s/g, "") == "") {
					this.elem.value = this.placeHolder;
				}
			},
			// 检测输入的字数
			check: function() {
				if (this.elem.value == this.placeHolder) {
					return;
				}

				var str = this.elem.value;
				var len = this._getInputLength(str);
				var showElem = document.getElementById(this.showId);
				
				if (len > this.maxLen * 2) {
					this.elem.value = str.substring(0, i + 1);
				} else {
					showElem.innerHTML = Math.floor((this.maxLen * 2 - len) / 2);
				}
			},
			/**
			 * 获取输入的字符数
			 * @param [string] str
			 * @return [number]
			 **/
			_getInputLength: function(str) {
				var len = 0;
				
				for (var i = 0; (i < str.length) && (len <= this.maxLen * 2); i++) {
					if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 128) {
						len++;
					} else {
						len += 2;
					}
				}

				return len;
			}
		};

		// 静态方法绑定
		ExTextArea.bind = function(elem, placeholder, showId, maxLen) {
			return new ExTextArea(elem, placeholder, showId, maxLen);
		};

		return ExTextArea;
	})();


	return InputEntity;
})();